/*
 *
 * Copyright(c) TODO
 * licensing@realtor.org
 * http://www.realtor.org/license
 */

Ext.onReady(function()
{
	/*AD.UI.comps_panel = new Ext.Panel(
	{
		title : 'Comps',
		frame : true,
		autoScroll : true,
		collapsible : true,
		width : AD.Globals.form_width,
		renderTo : 'div_comps',
		id : 'comps_panel'
	});*/


	AD.UI.comps_panel = new Ext.Window({
		title : 'Property Details',
		  id:'comps_panel',
		width: AD.Globals.client_width * 0.30,
		  height: AD.Globals.client_height * 0.60,

		  frame : true,
		  collapsible : true,
		  autoScroll:true,
		  closable : false,
		  minimizable: false,
		  maximizable: true,
		  stateful: true
	});

   AD.UI.comps_panel .show();

	AD.UI.comps_clear = function ()
	{
		var pnl = Ext.getCmp("comps_panel");
		pnl.items.clear();
		var i;
		while (i = pnl.items.first())
		{
			pnl.remove(i, true);
		}

		var tbl = Ext.get("id_comps_table");
		if(tbl) {	tbl.update("&nbsp;");};
		pnl.doLayout();
	};

	function make_remove_div(ml)
	{
		var html = "<div id=Remove_" + ml + "></div>";
		return html;
	}

	var focus_on_comp = false;
	function comps_set_focus()
	{
		//focus_on_comp = false;
		var el = document.getElementById('id_comps_scroll_target');
		if(el)
		{
			el.scrollIntoView(true);
		}
	}
	function fn_remove_comp(id)
	{
		var ind = AD.Data.ds_search.find('ml_number', id);
		var results_grid = Ext.getCmp('results_grid');

		//results_grid.getView().focusRow(ind);
		results_grid.getSelectionModel().deselectRow(ind);

		focus_on_comp = true;
		comps_set_focus();
	}

	var std_fields = [
		"street_number", "street_direction", "street_name",	"street_suffix", "street_post_direction", "unit",
		"city", "state","zip",
		"listing_price", "selling_price",
		"ml_number","status",
		"apn", "financing", "selling_date", "school_district",
		"acres", "view", "architecture",  "exterior", "age",
		"bed_rooms", "bath_rooms",
		"sqft_above_ground", "basement", "basement_feat",
		"heating", "cooling", "garage", "fireplaces"
	];

	function strcmp ( str1, str2 )
	{
		if(str1 == str2) { return 0; }
		if(str1 > str2) { return 1; }
		
		return -1;
		//return ( ( str1 == str2 ) ? 0 : ( ( str1 > str2 ) ? 1 : -1 ) );
	}

	function search_array(key, arr)
	{
		for (var i in arr)
		{
			if(0  == strcmp(key, arr[i]))
			{
				return true;
			}
		}
		return false;
	}
	var fn_get_xml_children = function (data)
	{
		var xp = "[@ml_number='" + data.ml_number + "']";
		var row = Ext.DomQuery.select(xp,	AD.Data.rdr_search.xmlData.documentElement);
		return row[0].childNodes;	
	}
	var fn_put_on_comp_hdr = function(dh, data)
	{
		// Standard
		str_html = '<table id="id_comps_table"  border="0" cellpadding="1" cellspacing="1" width="100px">';
		str_html += '<tr  id="ad_comps_row_img"   class="x-grid3-row"><td  class="x-grid3-cell-selected" id="id_comps_scroll_target">Image</td></tr>';
		str_html += '<tr  id="ad_comps_row_rem" class="x-grid3-row"><td  class="x-grid3-cell-selected"></td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_Address"> <td class="x-grid3-cell-selected nowrapper " >Address</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_listing_price" ><td class="x-grid3-cell-selected nowrapper">List Price</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_selling_price" ><td class="x-grid3-cell-selected nowrapper"  >Sale Price</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_ml_number" ><td class="x-grid3-cell-selected nowrapper">Data Source /ML Num</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_status" ><td class="x-grid3-cell-selected nowrapper">Status</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_apn" ><td class="x-grid3-cell-selected nowrapper">Verification Source/APN</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_financing" ><td class="x-grid3-cell-selected nowrapper">Financing</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_selling_date" ><td class="x-grid3-cell-selected nowrapper">Date of Sale</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_school_district" ><td class="x-grid3-cell-selected nowrapper">School District</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_acres" ><td class="x-grid3-cell-selected nowrapper">Acres</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_view" ><td class="x-grid3-cell-selected nowrapper">View</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_architecture" ><td class="x-grid3-cell-selected nowrapper">Design / Style</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_exterior" ><td class="x-grid3-cell-selected nowrapper">Quality of Construction</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_age" ><td class="x-grid3-cell-selected nowrapper">Age</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_rooms" ><td class="x-grid3-cell-selected nowrapper">Rooms</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_bed_rooms" ><td class="x-grid3-cell-selected nowrapper">Bedrooms</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_bath_rooms" ><td class="x-grid3-cell-selected nowrapper">Bathrooms</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_sqft_above_ground" ><td class="x-grid3-cell-selected nowrapper">Gross Living Area</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_basement" ><td class="x-grid3-cell-selected nowrapper">Basement & Finish</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_basement_feat" ><td class="x-grid3-cell-selected nowrapper">Rooms Below Grade</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_heating" ><td class="x-grid3-cell-selected nowrapper">Heating</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_cooling" ><td class="x-grid3-cell-selected nowrapper">Cooling</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_garage" ><td class="x-grid3-cell-selected nowrapper">Garage / Carport</td></tr>';
		str_html += '<tr class="x-grid3-row" id="ad_comps_row_fireplaces" ><td class="x-grid3-cell-selected nowrapper">Fireplaces</td></tr>';

		/*for ( var s = 0; s < std_fields.length; s++)
		{
			str_html += '<tr class="x-grid3-row " id="ad_comps_row_'
						+ std_fields[s]
						+ '" ><td class="x-grid3-cell-selected nowrapper">'
						+ std_fields[s] + '</td></tr>';
		}*/

		str_html += '<tr class="x-grid3-row" id="id_detail_splitter" ><td class="x-grid3-cell-selected">&nbsp;</td></tr>';

		var f  = fn_get_xml_children(data);
		var fld = "";
		for(i=0; i<f.length; i++)
		{
			fld = f[i].nodeName;
			if (false == search_array(fld, std_fields))
			{
				str_html += '<tr class="x-grid3-row " id="ad_comps_row_'+ fld + '" ><td class="x-grid3-cell-selected nowrapper">' +  AD.Format.format_label(fld)	+ '</td></tr>';
			}
		}
		
		/*for (var g in data)
		{
			if (false == search_array(g, std_fields))
			{
				str_html += '<tr class="x-grid3-row " id="ad_comps_row_'+ g + '" ><td class="x-grid3-cell-selected nowrapper">' +  AD.Format.format_label(g)	+ '</td></tr>';
			}
		}*/
		str_html += "</table>";

		dh.append('CC_GIGO',
		{
			tag : 'div',
			cls : 'tdl',
			html : str_html
		});
	};
	function get_pix_count(ml_number)
	{
		var id = AD.Data.ds_search.find('ml_number', ml_number);
		if(-1 == id)
		{
			return 0;
		}

		rec = AD.Data.ds_search.getAt(id);
		if(undefined == rec)
		{
			return 0;
		}

		return parseInt(rec.data.picture_count);
	}

	function get_img(ml_number, offset)
	{
		var el = Ext.get('cur_pix_' + ml_number);
		var pix_count = get_pix_count(ml_number);
		var i = el.getValue(true) + offset;
		var condition = (offset < 0) ? (0 <= i) : (i < pix_count);

		if (condition)
		{
			el.set({value : i});

			var url = AD.Globals.URL_server +  'img&img_index=' + i + '&ml_number=' + ml_number;
			var pix = Ext.get('pix_' + ml_number);
			pix.set({src : url});

			var elx = Ext.get('show_' + ml_number);
			elx.dom.innerHTML = ' ' + (i+1) + ' / ' + pix_count;
		}
	}

	var fn_put_on_comp = function(dh, data)
	{
		// Standard
		var str_html = ('<img  width="200px" id="pix_' + data.ml_number +  '"src="' + AD.Globals.URL_server + 'img&img_index=0&ml_number=' + data.ml_number + '" />');
		dh.append('ad_comps_row_img',
		{
			tag : 'td',
			cls : 'x-grid3-row',
			html : str_html
		});

		var img = (data.picture_count > 0) ? 1 : 0;
		str_html  = ('<table border=0><tr><td><div id="Remove_' + data.ml_number + '"></div></td>');
		str_html += ('<td width="50%"><input type=hidden id=cur_pix_' + data.ml_number + ' value="0"></td>');
		str_html += ('<td><div id="Prev_' + data.ml_number + '"></div></td>');
		str_html += ('<td style="white-space: nowrap;"><div id="show_' + data.ml_number + '">' + img + ' / '+ data.picture_count +'</div></td>');
		str_html += ('<td><div id="Next_' + data.ml_number + '"></div></div></td></tr></table>');

		dh.append('ad_comps_row_rem',
		{
			tag : 'td',
			cls : 'x-grid3-row ',
			html : str_html
		});

		try
		{
			var btn = new Ext.Button(
			{
				applyTo : ('Remove_' + data.ml_number),
				text : 'Remove',
				width: 60,
				listeners :
				{
					'click' : function()
					{
						fn_remove_comp(data.ml_number);
					}
				}
			});

			var btn = new Ext.Button(
					{
						applyTo : ('Prev_' + data.ml_number),
						text : '<<',
						width: 30,
						listeners :
						{
							'click' : function()
							{
								get_img(data.ml_number, -1);
							}
						}
					});

			var btn = new Ext.Button(
					{
						applyTo : ('Next_' + data.ml_number),
						text : '>>',
						width: 30,
						listeners :
						{
							'click' : function()
							{
								get_img(data.ml_number, +1);
							}
						}
					});
		} catch (e) {}

		var address = AD.Format.format_empty(data["street_number"])
					+ ' ' + AD.Format.format_empty(data["street_direction"])
					+ ' ' + AD.Format.format_empty(data["street_name"])
					+ ' ' + AD.Format.format_empty(data["street_suffix"])
					+ ' ' + AD.Format.format_empty(data["street_post_direction"])
					+ ' ' + AD.Format.format_empty(data["unit"])
					+ '<br>' + data["city"] + ', ' + data["state"] + ' ' + data["zip"];

		dh.append("ad_comps_row_Address",				{tag : 'td', cls : 'x-grid3-row', html : address});
		dh.append("ad_comps_row_listing_price", 		{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_money_00(data["listing_price"])});
		dh.append("ad_comps_row_selling_price", 		{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_money_00(data["selling_price"])});
		dh.append("ad_comps_row_ml_number", 			{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["ml_number"])});
		dh.append("ad_comps_row_status", 			{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["status"])});
		dh.append("ad_comps_row_apn",		 			{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["apn"])});
		dh.append("ad_comps_row_financing",		 	{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["financing"])});
		dh.append("ad_comps_row_selling_date", 		{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["selling_date"])});
		dh.append("ad_comps_row_school_district",	 	{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["school_district"])});
		dh.append("ad_comps_row_acres", 				{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["acres"])});
		dh.append("ad_comps_row_view", 				{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["view"])});
		dh.append("ad_comps_row_architecture",		 	{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["style"])});
		dh.append("ad_comps_row_exterior",			 	{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["exterior"])});

		dh.append("ad_comps_row_age",				 	{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["year_built"])});
		dh.append("ad_comps_row_rooms",			 	{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["rooms"])});
		dh.append("ad_comps_row_bed_rooms",		 	{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["bed_rooms"])});
		dh.append("ad_comps_row_bath_rooms",		 	{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["bath_rooms"])});
		dh.append("ad_comps_row_sqft_above_ground", 	{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["sqft_above_ground"])});
		dh.append("ad_comps_row_basement",			 	{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["basement"])});
		dh.append("ad_comps_row_basement_feat",	 	{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["basement_feat"])});
		dh.append("ad_comps_row_heating", 				{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["heating"])});
		dh.append("ad_comps_row_cooling",			 	{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["cooling"])});
		dh.append("ad_comps_row_garage", 				{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["garage"])});
		dh.append("ad_comps_row_fireplaces", 			{tag : 'td', cls : 'x-grid3-row', html : AD.Format.format_empty(data["fireplaces"])});
		dh.append('id_detail_splitter', 				{tag : 'td', cls : 'x-grid3-cell-selected', html : '&nbsp;'});

		/*for (var g in data)
		{
			if (false == search_array(g, std_fields))
			{
				cell = "ad_comps_row_" + g;
				dh.append(cell, {tag : 'td', cls : 'x-grid3-row',html : AD.Format.format_empty(data[g]) });
			}
		}*/
		var f  = fn_get_xml_children(data);
		var fld = "";
		for(i=0; i<f.length; i++)
		{
			fld = f[i].nodeName;
			if (false == search_array(fld, std_fields))
			{
				cell = "ad_comps_row_" + fld;
				//dh.append(cell, {tag : 'td', cls : 'x-grid3-row',html : AD.Format.format_empty(data[f[i].text]) });
				dh.append(cell, {tag : 'td', cls : 'x-grid3-row',html : AD.Format.format_empty(f[i].text) });
		
				
			}
		}
	};

	function display_details(i, dh, rec)
	{
		if (i == 0)
		{
			fn_put_on_comp_hdr(dh, rec.data);
		}

		fn_put_on_comp(dh, rec.data);

		if (AD.UI.map_bing)
		{
			AD.UI.fn_put_on_map_bing(rec.data);
		}

		if (AD.UI.map_goog)
		{
			AD.UI.fn_put_on_map_goog(rec.data);
		}

		if(true == focus_on_comp)
		{
			comps_set_focus();
			focus_on_comp = false;
		}
	}

	AD.UI.comps_panel.fn_load_comps_detail = function()
	{
		var sel = AD.UI.get_sels_from_grid('results_grid');
		var len = sel.length;

		if ((!len) || (len == 0))
			return;

		var dh = Ext.DomHelper;
		var recid;
		//alert('Clear');
		
		AD.UI.map_bing_clear();
		AD.UI.map_goog_clear();
		
		for (var i = 0; i < len; i++)
		{
			recid = sel[(len - i - 1)].id;
			rec = AD.Data.ds_search.getById(recid);

			if (!rec)
			{
				continue;
			}
			else
			{
				display_details(i, dh, rec);
			}
		}
	};

	AD.UI.comps_panel.fn_load_comps_detail_all = function()
	{
		var dh = Ext.DomHelper;
		var i = 0;
		AD.Data.ds_search.each(function(rec)
		{
			display_details(i, dh, rec);
			i++;
		});
	};
});
